package com.edushare.common.mailer;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.edushare.common.vo.OutgoingEmails;

public abstract class MailStringLoader {
	
	public static String templateParser(String template, HashMap<String, String> contentVariableMap){
		StringBuffer sb = new StringBuffer();
		String regexPattern = "<es:replace>[a-zA-Z0-9_\\-]*</es:replace>";
		Pattern p = Pattern.compile(regexPattern);
		Matcher m = p.matcher(template);
		while(m.find()){
			String key = m.group().replace("<es:replace>", "");
			key = key.replace("</es:replace>", "");
			String replacement = contentVariableMap.get(key);
			if(replacement==null)replacement = "Entity";
			m.appendReplacement(sb, replacement);
		}
		m.appendTail(sb);
		return sb.toString();
	}
	
	public abstract OutgoingEmails send();
	
	public String messageString = "";
	protected HashMap<String, String> replaceMap = new HashMap<String, String>();
	protected MailBase mailBase = MailBase.get();
}
